/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controlador;

import java.awt.Color;
import java.awt.Graphics;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JProgressBar;
import modelo.Caldera;
import modelo.Llave;
import modelo.Tanque;

/**
 *
 * @author sebastian
 */
public class Controlador extends javax.swing.JPanel implements Runnable{

    private Tanque tanque;
    private Llave q1;
    private Llave q2;
    private Caldera caldera;
    private JProgressBar medTemperatura;
    private JProgressBar medCaudalQ1;
    private JProgressBar medCaudalQ2;
    
    private int velocidadHilo;
    
    public Controlador() {
        initComponents();
    }
    
    public void recibir(Tanque tanque, Llave q1, Llave q2, Caldera caldera, 
            JProgressBar medTemperatura, JProgressBar medCaudalQ1, JProgressBar medCaudalQ2, int velocidad){
        
        this.tanque = tanque;
        this.q1 = q1;
        this.q2 = q2;
        this.caldera = caldera;
        this.medTemperatura = medTemperatura;
        this.medCaudalQ1 = medCaudalQ1;
        this.medCaudalQ2 = medCaudalQ2;
        this.velocidadHilo = 1000/velocidad;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
        if (this.tanque != null) {
            g.setColor(Color.BLUE);
            g.drawImage(this.tanque.getImagenTanque().getImage(), 50, 100, 400, 480, null);
            g.drawImage(this.tanque.getImagenLiquido().getImage(), this.tanque.getNivelX(),
                    this.tanque.getNivelY() - this.tanque.getHeight(), 180, this.tanque.getHeight(), null);
            repaint();
        }
    }
    
    @Override
    public void run() {
        while(true){
            
            if (this.tanque.getNivelActual() < (this.tanque.getNivelMaximo()*0.6)) {
                this.q1.aumentarNivelApertura(1);
                float caudal = this.q1.getCaudal();
                this.tanque.cambiarNivel(caudal, 1);    
                 this.q2.disminuirNivelApertura(0);
             
                if(this.q2.getCaudal()>0){
                    this.tanque.cambiarNivel(this.q2.getCaudal(), -1);
                }
                
//                if (this.tanque.getTemperatura() < 100) {
//                    //this.caldera.cambiarTemperatura(20, 1);
//                    float nuevaTemp = (((this.tanque.getNivelActual()) * this.tanque.getTemperatura()) + 
//                            (this.q1.getCaudal() * 25))/(int)(this.tanque.getNivelActual());
//                    this.tanque.setTemperatura(nuevaTemp);
//                    //this.tanque.recalcularTemperatura(this.caldera.getPotencia());
//                }
            }
            else{
                if (this.tanque.getNivelActual() >= (this.tanque.getNivelMaximo()*0.6) && 
                        this.tanque.getNivelActual() < (this.tanque.getNivelMaximo()*0.65)){
                    
                    // Abrir llave q2
                    this.q2.aumentarNivelApertura(0);
                    float caudalQ2 = this.q2.getCaudal();
                    this.tanque.cambiarNivel(caudalQ2, -1);
                    
                    // cerrar llave q1
                    this.q1.disminuirNivelApertura(1);
                    float caudalQ1 = this.q1.getCaudal();
                    this.tanque.cambiarNivel(caudalQ1, 1);
                    
                    
//                    if (this.tanque.getTemperatura() < 100) {
//                        this.caldera.cambiarTemperatura(20, 1);
//                        this.tanque.recalcularTemperatura(this.caldera.getPotencia());
//                    }
                }
            }         
            
            try {
                Thread.sleep(this.velocidadHilo);
            } catch (InterruptedException ex) {
                Logger.getLogger(Controlador.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    public void cambiarVelocidad(int velocidad){
        this.velocidadHilo = 1000/velocidad;

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setBackground(new java.awt.Color(102, 102, 102));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 434, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 314, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}